#include <REGX52.H>
#include "Delay.h"
#include "Nixie.h"
#include "Timer0.h"
//#include "MatriKey.h"
//蜂鸣器端口：
sbit Buzzer=P2^5;
unsigned char FreqNum;
unsigned char Sound;
unsigned int FreqList[]=
{
	64580, 
	64685, 
	64778, 
	64820, 
	64898, 
	64968, 
	65030,
	65058
};

void KeyBoard()
{
	//unsigned char KeyNumber=0;
	Sound=0;
	P1=0xFF;
	P1_3=0;
	if(P1_7==0)
		{	
			FreqNum=1;
			Nixie(1,1);
			Sound=1;
			//0说明按钮按下，如果按键按下不放，程序会停留在此一直循环，直到松手的一瞬间，变成非零跳出循环
			//Sound=0;//按键键码赋值1
		}
	if(P1_6==0){FreqNum=5;Nixie(1,5);Sound=1;}
	if(P1_5==0){FreqNum=9;Nixie(1,9);Sound=1;}
	if(P1_4==0){FreqNum=13;Nixie(1,13);Sound=1;}
	
	P1=0xFF;
	P1_2=0;
	if(P1_7==0){FreqNum=2;Nixie(1,2);Sound=1;}
	if(P1_6==0){FreqNum=6;Nixie(1,6);Sound=1;}
	if(P1_5==0){FreqNum=10;Nixie(1,10);Sound=1;}
	if(P1_4==0){FreqNum=14;Nixie(1,14);Sound=1;}
	
	P1=0xFF;
	P1_1=0;
	if(P1_7==0){FreqNum=3;Nixie(1,3);Sound=1;}
	if(P1_6==0){FreqNum=7;Nixie(1,7);Sound=1;}
	if(P1_5==0){Sound=0;}
	if(P1_4==0){Sound=0;}
	
	P1=0xFF;
	P1_0=0;
	if(P1_7==0){FreqNum=4;Nixie(1,4);Sound=1;}
	if(P1_6==0){FreqNum=8;Nixie(1,8);Sound=1;}
	if(P1_5==0){Sound=0;}
	if(P1_4==0){Sound=0;}
	
	//return FreqNum;
}

void main()
{
	Nixie(1,0);
	Timer0Init();
	while(1)
	{
//		KeyBoard();
	}
}


void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count;
	TL0 = FreqList[FreqNum-1]%256;		//设置定时初值
	TH0 = FreqList[FreqNum-1]/256;		//设置定时初值
	if(Sound){Buzzer=!Buzzer;}	//翻转蜂鸣器IO口
	//KeyBoard();
	T0Count++;
	if(T0Count>=20)
	{
		T0Count=0;
		KeyBoard();
	}
}